/* 增删改查四个与tasks表交互的方法 */
const Database=require('better-sqlite3')
const path = require('path');
const db = new Database(path.join(__dirname, 'app.db'));


function getAllByUserId(userId) {
  const rows = db.prepare('SELECT * FROM tasks WHERE user_id = ?').all(userId);
  return rows;
}


function addTask(text,id){
  try{
    db.prepare('insert into tasks(text,user_id) values(?,?)').run(text,id)
    return {status:0,msg:'ok'}
  }catch(e){
    return {status:12,msg:e.message}
  }
  

}

function removeById(id){
  try{
    db.prepare('delete from tasks where id=?').run(id)
    return {status:0,msg:'ok'}
  }catch(e){
    return {status:12,msg:e.message}
  }
  
}

function updateById(text,id){
  try{
    db.prepare('update tasks set text=? where id=?').run(text,id)
    return {status:0,msg:'ok'}
  }catch(e){
    return {status:12,msg:e.message}
  }
  
}

module.exports = {
    getAllByUserId,

    addTask,
    removeById,
    updateById
}